#---------------------------------------------------------------
# City-Defined Neighborhoods Database
#
# 02_map.R
# Creates Figure 1.
#---------------------------------------------------------------

library(tidyverse)
library(sf)
library(urbnmapr)

data_list <- list.files("cdnd_data/",
                        recursive = TRUE)

rds_list <- data_list[str_detect(data_list, fixed(".rds"))]

#--------------------------------------------------------------

all_city <- lapply(1:length(rds_list), function(l) {
  x <- read_rds(paste0("cdnd_data/",
                  rds_list[l]))
  unique(paste0(x$state, x$place))
})

unlist(all_city)

places <- tigris::places(NULL, cb = TRUE)

place_sf <- places %>%
  filter(GEOID %in% unlist(all_city))

#-----------------------------------------------------------
states_sf <- tigris::states()

states_sf <- states_sf %>%
  filter(!(STATEFP %in% c("02", "15"))) %>%
  filter(!(NAME %in% c("United States Virgin Islands",
                       "Commonwealth of the Northern Mariana Islands",
                       "Guam",
                       "American Samoa",
                       "Puerto Rico")))

states_sf <- get_urbn_map("states", sf = TRUE) %>%
  filter(!(state_name %in% c("Alaska", "Hawaii")))

place_sf <- st_centroid(place_sf)

place_sf$label <- paste0(place_sf$NAME, ", ", place_sf$STUSPS)

place_sf <- place_sf %>% filter(!(STUSPS %in% c("AK", "HI")))

outline <- states_sf %>% summarize(geometry = st_union(geometry))

place_sf$label[place_sf$label == "Boise City, ID"] <- "Boise, ID"

states_sf <- st_transform(states_sf, crs = "EPSG:9311")
outline <- st_transform(outline, crs = "EPSG:9311")
place_sf <- st_transform(place_sf, crs = "EPSG:9311")

pmap <- place_sf %>%
  ggplot() +
  geom_sf(data = states_sf, fill = "#E2DFDF", color = "black") +
  geom_sf(data = outline, fill = NA, linewidth = 0.75, color = "black") +
  geom_sf(size = 4,
          color = "#DB331A", alpha = 0.7) +
  geom_sf_label(data = place_sf %>% filter(label %in%
                                             c("Boston, MA",
                                               "New York, NY",
                                               "Denver, CO",
                                               "Houston, TX",
                                               "Los Angeles, CA",
                                             "Jacksonville, FL")),
                aes(label = label),
                color = "black", fontface = "bold") +
  geom_sf_label(data = place_sf %>% filter(label %in%
                                             c("Ann Arbor, MI",
                                               "Birmingham, AL",
                                               "Peoria, IL",
                                               "Billings, MT",
                                               "Boise, ID",
                                               "Carrollton, TX",
                                               "Pompano Beach, FL",
                                               "Tulsa, OK",
                                               "Bismarck, ND",
                                               "Wilmington, NC")),
                aes(label = label),
                color = "black") +
  guides(fill = "none") +
  ggthemes::theme_map() +
  theme(plot.margin=grid::unit(c(0,0,0,0), "mm"))

ggsave("output/sample_map.pdf", pmap, width = 9, height = 6)
